<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>dataTransfer.items during link drag and drop</title>
<style type="text/css">
div
  {height:100px;
  width:100px;
  padding:20px;
  background-color:silver;}
</style>
<script type="application/ecmascript">
<![CDATA[
var dataTypes = ['text/uri-list', 'text/plain', 'application/xml', 'application/xhtml+xml', 'application/mathml+xml', 'image/svg+xml', 'text/html', 'text/x-example'],
data = ['data:text/plain,1', 'PASS', '<result>PASS</result>', '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Data store item</title></head><body><p>PASS</p></body></html>', '<math xmlns="http://www.w3.org/1998/Math/MathML"><mn>1</mn></math>', '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100px" height="50px" viewBox="0 0 100 50"><text x="0" y="40" font-size="40" fill="green">PASS</text></svg>', '<!DOCTYPE html><html><head><title>Data store item</title></head><body><p>PASS</p></body></html>', 'PASS'],
e = 0, result = true;
function start(event)
  {event.dataTransfer.effectAllowed = 'copy';
  for(var i = 0; i != dataTypes.length; i++)
    {event.dataTransfer.setData(dataTypes[i], data[i]);}
  for(var i = event.dataTransfer.items.length; i != 0; i--)
    {if(dataTypes.indexOf(event.dataTransfer.items[i-1].type) == -1)
      {delete event.dataTransfer.items[i-1]}
    }
  if(event.dataTransfer.items.length != dataTypes.length)
      {say('items.length (ondragstart) : FAIL (items.length should be' + dataTypes.length + ')')}
  for(var i = 0; i != event.dataTransfer.items.length; i++)
    {if(event.dataTransfer.items[i].kind != 'string')
      {say('Item kind (ondragstart): FAIL (items[' + i + '].kind should be string)')}
    if(event.dataTransfer.items[i].type != dataTypes[i])
      {say('Item type (ondragstart): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
    event.dataTransfer.items[i].getAsString(
      function ()
        {if(arguments[0] != data[e++])
          {say('getAsString : FAIL (items[' + i + '].getAsString should pass' + data[e] + ')')}
        }
      );
    }
  }
function dragElement(event)
  {event.dataTransfer.items.clear();
  if(event.dataTransfer.items.length != dataTypes.length)
    {say('items.length (ondrag) : FAIL (items.length should be' + dataTypes.length + ')')}
  for(var i = 0; i != event.dataTransfer.items.length; i++)
    {if(event.dataTransfer.items[i].kind != 'string')
      {say('Item kind (ondrag): FAIL (items[' + i + '].kind should be string)')}
    if(event.dataTransfer.items[i].type != dataTypes[i])
      {say('Item type (ondrag): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
    event.dataTransfer.items[i].getAsString(
      function ()
        {if(arguments[0])
          {say('getAsString : FAIL (items[' + i + '].getAsString should not reveal data to drag event handler)')}
        }
      );
    }
  }
function enterElement(event)
  {event.preventDefault();
  event.dataTransfer.items.clear();
  if(event.dataTransfer.items.length != dataTypes.length)
    {say('items.length (ondragenter) : FAIL (items.length should be' + dataTypes.length + ')')}
  for(var i = 0; i != event.dataTransfer.items.length; i++)
    {delete event.dataTransfer.items[i];
    if(event.dataTransfer.items[i].kind != 'string')
      {say('Item kind (ondragenter): FAIL (items[' + i + '].kind should be string)')}
    if(event.dataTransfer.items[i].type != dataTypes[i])
      {say('Item type (ondragenter): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
    event.dataTransfer.items[i].getAsString(
      function ()
        {if(arguments[0])
          {say('getAsString : FAIL (items[' + i + '].getAsString should not reveal data to drag event handler)')}
        }
      );
    }
  }
function overElement(event)
  {event.preventDefault();
  event.dataTransfer.items.clear();
  if(event.dataTransfer.items.length != dataTypes.length)
    {say('items.length (ondragover) : FAIL (items.length should be' + dataTypes.length + ')')}
  for(var i = 0; i != event.dataTransfer.items.length; i++)
    {if(event.dataTransfer.items[i].kind != 'string')
      {say('Item kind (ondragover): FAIL (items[' + i + '].kind should be string)')}
    if(event.dataTransfer.items[i].type != dataTypes[i])
      {say('Item type (ondragover): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
    event.dataTransfer.items[i].getAsString(
      function ()
        {if(arguments[0])
          {say('getAsString : FAIL (items[' + i + '].getAsString should not reveal data to dragover event handler)')}
        }
      );
    }
  }
function dataDrop(event)
  {e = 0;
  event.dataTransfer.items.clear();
  if(event.dataTransfer.items.length != dataTypes.length)
      {say('items.length (ondrop) : FAIL (items.length should be' + dataTypes.length + ')')}
  for(var i = 0; i != event.dataTransfer.items.length; i++)
    {delete event.dataTransfer.items[i];
    if(event.dataTransfer.items[i].kind != 'string')
      {say('Item kind (ondrop): FAIL (items[' + i + '].kind should be string)')}
    if(event.dataTransfer.items[i].type != dataTypes[i])
      {say('Item type (ondrop): FAIL (items[' + i + '].type should be' + dataTypes[i] + ')')}
    event.dataTransfer.items[i].getAsString(
      function ()
        {if(arguments[0] != data[e++])
          {say('getAsString : FAIL (items[' + i + '].getAsString should pass' + data[e] + ')')}
        }
      );
    }
  document.querySelector('div').setAttribute('style','background-color:' + (result?'green':'red'));}
function say(it)
  {result = false;
  document.querySelector('pre').appendChild(document.createTextNode(it + '\n'));}
]]>
</script>
</head>
<body>
<p><a href="data:text/plain,1" ondragstart="start(event)" ondrag="dragElement(event)">Drag me</a></p>
<p>Drag link above to the silver box below and drop it. Silver box should turn green.</p>
<div ondragenter="enterElement(event)" ondragover="overElement(event)" ondrop="dataDrop(event)"/>
<pre/>
</body>
</html>
